package cn.b.sky.mes.qm.controller;

import java.util.List;

import javax.annotation.Resource;

import cn.b.sky.mes.qm.model.DefectNode;
import cn.b.sky.mes.qm.service.DefectNodeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.b.sky.common.constants.SystemConstant;
import cn.b.sky.common.exception.ImeException;
import cn.b.sky.common.model.DWZResult;
import cn.b.sky.common.model.TreeInterface;
import cn.b.sky.user.SecurityContextUtil;
import cn.b.sky.user.controller.SecurityController;
import cn.b.sky.mes.cm.enums.ECodeEnums;
import cn.b.sky.user.service.ECodeService;
import cn.b.sky.user.service.model.ECode;
import cn.b.sky.mes.qm.enums.DefectNodeType;

/**
 * 
 * <strong>Title : DefectNodeController.java<br></strong>
 * <strong>Package : cn.b.sky.mes.qm.controller<br></strong>
 * <strong>Description : </strong>缺陷树控制器<br> 
 * <strong>Create on : 2015-12-16 上午11:02:05<br></strong>
 * <p>
 * @author lidapeng<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 * <strong>修改历史:</strong><br>
 * 修改人 | 修改日期 | 修改描述<br>
 * -------------------------------------------<br>
 * <br>
 * <br>
 */
@Controller
@RequestMapping("/defectNode/*")
public class DefectNodeController extends SecurityController {

  @Resource
  private DefectNodeService defectNodeService;

  @Resource
  private ECodeService eCodeService;

  @RequestMapping("defectNodeTree")
  public String defectNodeTree(Model model, String navTabId) {

    List<TreeInterface> defectNodeTree = defectNodeService.getDefectNodeTree(this.getSiteCode());
    model.addAttribute("defectNodeTree", defectNodeTree);
    model.addAttribute(NAVTABID, navTabId);
    return "qm/defectNode/defectNode_tree";
  }

  @RequestMapping("addDefectNode")
  public String addDefectNode(Model model, String navTabId, String parentDefectNodeCode) {
    if (!StringUtils.isBlank(parentDefectNodeCode)) {
      DefectNode parentDefectNode = defectNodeService.getDefectNodeByNodeCodeAndSiteCode(this.getSiteCode(),
          parentDefectNodeCode);
      model.addAttribute("parentDefectNode", parentDefectNode);
    }

    List<ECode> eCode = eCodeService.selectECodeList(ECodeEnums.QM_DEFECTNODETYPE.getCode(),
        SecurityContextUtil.getLanguageCode());
    model.addAttribute("defectNodeTypes", eCode);

    model.addAttribute("parentDefectNodeCode", parentDefectNodeCode);
    model.addAttribute(NAVTABID, navTabId);
    return "qm/defectNode/defectNode_add";
  }

  @RequestMapping("saveDefectNode")
  @ResponseBody
  public DWZResult saveDefectNode(Model model, String navTabId, DefectNode defectNode) {
    if (defectNode == null || StringUtils.isBlank(defectNode.getNodeCode())) {
      return ajaxError(getImeViewMessageI18n(SecurityContextUtil.getLanguageCode(), SystemConstant.PARAMETERNULL));
    }
    defectNode.setSiteCode(this.getSiteCode());
    setCreateInfo(defectNode);
    defectNodeService.saveDefectNode(defectNode);
    return navTabAjaxDone(navTabId, null, null, null, null, null);
  }

  @RequestMapping("modifyDefectNode")
  @ResponseBody
  public DWZResult modifyDefectNode(Model model, String navTabId, DefectNode defectNode) {
    if (defectNode == null || StringUtils.isBlank(defectNode.getNodeCode())) {
      return ajaxError(getImeViewMessageI18n(SecurityContextUtil.getLanguageCode(), SystemConstant.PARAMETERNULL));
    }
    defectNode.setSiteCode(this.getSiteCode());
    setCreateInfo(defectNode);
    defectNodeService.modifyDefectNode(defectNode);
    return navTabAjaxDone(navTabId, null, null, null, null, null);
  }

  @RequestMapping("editDefectNode")
  public String editDefectNode(Model model, String navTabId, String nodeCode) {
    if (StringUtils.isBlank(nodeCode)) {
      throw ImeException.newException(SystemConstant.PARAMETERNULL);
    }
    DefectNode defectNode = defectNodeService.getDefectNodeByNodeCodeAndSiteCode(this.getSiteCode(), nodeCode);
    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute("defectNode", defectNode);
    List<ECode> eCode = eCodeService.selectECodeList(ECodeEnums.QM_DEFECTNODETYPE.getCode(),
        SecurityContextUtil.getLanguageCode());
    model.addAttribute("defectNodeTypes", eCode);
    if (!StringUtils.isBlank(defectNode.getParentNodeCode())) {
      DefectNode parentDefectNode = defectNodeService.getDefectNodeByNodeCodeAndSiteCode(this.getSiteCode(),
          defectNode.getParentNodeCode());
      model.addAttribute("parentDefectNode", parentDefectNode);
    }
    return "qm/defectNode/defectNode_edit";
  }

  @RequestMapping("viewDefectNode")
  public String viewDefectNode(Model model, String navTabId, String nodeCode) {
    if (StringUtils.isBlank(nodeCode)) {
      throw ImeException.newException(SystemConstant.PARAMETERNULL);
    }
    DefectNode defectNode = defectNodeService.getDefectNodeByNodeCodeAndSiteCode(this.getSiteCode(), nodeCode);
    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute("defectNode", defectNode);
    List<ECode> eCode = eCodeService.selectECodeList(ECodeEnums.QM_DEFECTNODETYPE.getCode(),
        SecurityContextUtil.getLanguageCode());
    model.addAttribute("defectNodeTypes", eCode);
    if (!StringUtils.isBlank(defectNode.getParentNodeCode())) {
      DefectNode parentDefectNode = defectNodeService.getDefectNodeByNodeCodeAndSiteCode(this.getSiteCode(),
          defectNode.getParentNodeCode());
      model.addAttribute("parentDefectNode", parentDefectNode);
    }
    return "qm/defectNode/defectNode_view";
  }

  @RequestMapping("isExistsDefectNode")
  @ResponseBody
  public Boolean isExistsDefectNode(Model model, String nodeCode) {
    if (StringUtils.isBlank(nodeCode)) {
      return false;
    }
    DefectNode defectNode = defectNodeService.getDefectNodeByNodeCodeAndSiteCode(this.getSiteCode(),
        StringUtils.upperCase(nodeCode));
    if (defectNode != null && !StringUtils.isBlank(defectNode.getNodeCode())) {
      return false;
    }
    return true;
  }

  @RequestMapping("deleteDefectNode")
  @ResponseBody
  public DWZResult deleteDefectNode(Model model, String navTabId, String nodeCode) {
    if (StringUtils.isBlank(nodeCode)) {
      throw ImeException.newException(SystemConstant.PARAMETERNULL);
    }
    DefectNode defectNode = defectNodeService.getDefectNodeByNodeCodeAndSiteCode(this.getSiteCode(), nodeCode);
    if (DefectNodeType.STRUCTURE.getValue().equals(defectNode.getDefectCode())) {
      List<DefectNode> childrens = defectNodeService.getChildrensNode(this.getSiteCode(), nodeCode);
      if (childrens != null && childrens.size() > 0) {
        return ajaxError(getImeViewMessageI18n(SecurityContextUtil.getLanguageCode(), "Tree_delete_msg.D"));
      }
    }
    defectNodeService.deleteDefectNodeByNodeCodeAndSiteCode(this.getSiteCode(), nodeCode);
    return ajaxDone(null);
  }

}
